package com.gsy.springai1.controller;


import com.gsy.springai1.entity.vo.MessageVO;
import com.gsy.springai1.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin("*")
@RestController
@RequestMapping("/ai/history")
@RequiredArgsConstructor
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final ChatMemory chatMemory;

    /**
     * 查询会话列表
     * @param type
     * @return
     */
    @RequestMapping("/{type}")
    public List<String> getIds(@PathVariable("type") String type) {

        return chatHistoryRepository.getChatIds(type);
    }

    /**
     * 查询会话记录详情
     * @param type
     * @param chatId
     * @return
     */
    @RequestMapping("/{type}/{chatId}")
    public List<MessageVO> getDetails(@PathVariable("type") String type , @PathVariable("chatId") String chatId) {

        List<Message> messages = chatMemory.get(chatId, 100);
        if (messages == null) {
            return List.of();
        }
        return messages.stream().map(message -> new MessageVO(message)).toList();
    }




}
